#include <iostream>

#include <2DesaCore>
#include <2DesaSDL>
#include <2DesaExt>

using namespace lib2Desa;

class MyListener : public IEventListener, public IGameRule
{

s32 keepRunning;

public:

	MyListener(void)
	{
		keepRunning = 1;
	}

	void notify(shared_ptr<IEvent> e);
	s32 eval(void);
};

void MyListener::notify(shared_ptr<IEvent> e)
{
	if(e->getType() == IEvent::ET_WINDOW)
	{
		keepRunning = 0;
		e->consume();
	}

	if(e->getType() == IEvent::ET_KEY)
	{
		Event<KeyMessage>* ke = (Event<KeyMessage>*) ((IEvent*)e);
		if(ke->getMessage().getKey() == KeyMessage::KMK_ESCAPE)
		{
			keepRunning = 0;
			e->consume();
		}
	}
}

s32 MyListener::eval(void)
{
	IGameRule::eval();
	return keepRunning;
}

void main_end(){}

int main(int argc, char** args)
{
	shared_ptr<IGraphicDevice> ggd;
	ggd = new SDL::GraphicDevice;

	if(!ggd) std::cout << "Falhou ao criar o device!\n";

	shared_ptr<IEventNotifier> enf = new SDL::EventNotifier;

	shared_ptr<MyListener> mL = new MyListener;
	enf->addListener(mL);

	ggd->initScreen(800,600);

	ggd->setClearColor(Color(0,0,255,0));

	shared_ptr<ISprite> im3 = ggd->load("data/fastdic_11899455586322.jpg");

	shared_ptr<Animation> an1;
	an1 = load_animation("data/quim_anda_frente.dani", ggd);

	shared_ptr<SimpleChar> o = new SimpleChar;

	o->setDimension(vector2f(10,10));

	o->setAnimation(0,an1);

	o->setRelPos(vector2f(200,200));

	o->enableAnimation(0,true);

	enf->addListener(o);

	shared_ptr<GameObject> p = new GameObject;
	p->setDimension(vector2f(1800,1));
	p->setRelPos(vector2f(-500,500));

	shared_ptr<GameObjectGroup> group = new GameObjectGroup;

	group->add(o);
	group->add(p);

	shared_ptr<Scene> sc = new Scene;;
	shared_ptr<CollisionSimpleRule> col_rule = new CollisionSimpleRule;

	sc->addGroup(group);
	sc->addRule(col_rule);
	sc->setBackground(im3);

	shared_ptr<ASync> gs;
	gs = new SDL::Sync(8);

	if(!gs) std::cout << "Sync nao foi criado!\n";

	shared_ptr<Game> game = new Game(ggd);

	game->addEventNotifier(enf);

	game->setScene(sc);
	game->setCloseRule(mL);

	game->setSync(gs);

	game->run();

	main_end();

	return 0;
}
